package com.bidevalution.business.job;

import java.util.Map;
import org.springframework.util.StringUtils;
import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.util.MapUtil;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Maps;

/**
 * 将某个环节的计算结果同步到另外一个环节结果
 * 
 * @author daizhifeng
 * @date 2019年10月16日 下午9:11:32
 * @version V2.0
 */
public class StepSyncResultDataJob implements StepJobJdbcFactory {
	private JdbcTemplateService jdbcTemplateService;

	@Override
	public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId,
			String layoutCode) throws Exception {
		// jobContent格式:sectionStepCode,layoutCode,fromColumnCode,toColumnCode
		if (!StringUtils.isEmpty(beSectionStepJob.getJobContent())) {
			String[] paramArray = StringUtils.delimitedListToStringArray(beSectionStepJob.getJobContent(), "|");
			String[] updateArray;
			for (String param : paramArray) {
				updateArray = StringUtils.delimitedListToStringArray(param, ",");
				if (updateArray.length != 4) {
					return false;
				}
				for (Map<String, Object> dataMap : jdbcTemplateService.selectForList(
						"select enterprise_id,column_value  from be_evalution_expert where section_step_id=:sectionStepId and column_code=:columnCode and expert_id=:expertId  and layout_code=:layoutCode and is_del='0'",
						MapUtil.create().putValue("sectionStepId", beSectionStepJob.getSectionStepId())
								.putValue("columnCode", updateArray[2]).putValue("layoutCode", layoutCode)
								.putValue("expertId", expertId).getMap())) {
					jdbcTemplateService.update(
							"update be_evalution_expert set column_value=:columnValue where enterprise_id=:enterpriseId and layout_code=:layoutCode and column_code=:columnCode and section_step_code=:sectionStepCode and is_del='0'",
							MapUtil.create().putValue("sectionStepCode", updateArray[0])
									.putValue("columnCode", updateArray[3])
									.putValue("columnValue", dataMap.get("column_value"))
									.putValue("enterpriseId", dataMap.get("enterprise_id"))
									.putValue("layoutCode", updateArray[1])
									.getMap());
				}
			}

			return true;
		}
		return false;
	}

	@Override
	public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
		this.jdbcTemplateService = jdbcTemplateService;
	}

}
